﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace Twi.COMMON.Core
{
    /*
     * CopyRight:©2012 Twilight软件开发工作室版权所有
     * Author:xuzhihong
     * Create Date: 2012-07-24
     * Description: 获取路径
     */
    public class TwiPathHelper
    {
        /// <summary>
        /// 获取绝对路径
        /// </summary>
        /// <param name="strPath">相对路径</param>
        /// <returns></returns>
        public static string MapPath(string strPath)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //多线程、非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("~")) {
                    strPath = strPath.TrimStart('~');
                }
                if (strPath.StartsWith("\\"))
                {
                    //strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                    strPath = strPath.TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }

        /// <summary>
        /// 判断指定文件是否存在
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <param name="isThrowException">如果检测到文件不存在，是否抛出异常</param>
        /// <param name="exceptionMsg">如果抛出异常，可设置的异常信息。默认为：“目标文件不存在”</param>
        /// <returns></returns>
        public static bool ExistFile(string fileFullPath,bool isThrowException=false,string exceptionMsg="目标文件不存在")
        {
            bool isExist = File.Exists(fileFullPath);
            if (isThrowException && !isExist)
            {
                throw new Exception(exceptionMsg);
            }
            return isExist;
        }
    }
}
